package cn.zbmao.web.services;

import cn.zbmao.fc.FcTool;

import java.util.*;

/**
 * 一些分析处理方法
 * Created by 愤怒的番茄 on 2014/10/7.
 */
public class AnalyzerServices {

    private static Map<String,List<String>> relatedWordAllMap = new HashMap<String, List<String>>();

    public static void addRelatedWordMap(String key,List<String> list) {
        relatedWordAllMap.put(key,list);

    }
    public static Boolean containsRelatedWordAllMapKey(String key){
        return relatedWordAllMap.containsKey(key);
    }
    public static List<String> getRelatedWordAllList(String key){
        return relatedWordAllMap.get(key);
    }

    public static void main(String[] args) {
        analyzerRelatedWords("恩施州纪委监控及审讯系统高清化改造工程 恩施州纪委监控及审讯系统高清化改造工程 高中部监控门禁系统维护消防设备更新采购 武汉市排污企业污染源自动监控设施建设项目询价采购公告 动力监控系统升级开发服务采购二次招标公告 麻城铁路公安处视频监控设备采购项目 武汉市中级人民法院安防监控系统采购 武汉市中级人民法院安防监控系统采购 湖北民院校园监控五期建设项目",new String[]{"系统"});
    }

    //相关词统计
    public static List<Map.Entry<String, Integer>> analyzerRelatedWords(String conntent,String[] strings){
        Set<String> set = new HashSet<String>();
        for (String string :strings){
            if (string.length()<=1){
                continue;
            }
            set.add(string);
        }

        return sortSegmentResult(getRelatedWordsDef(conntent,set),5);
    }

    //排序
    private static List<Map.Entry<String, Integer>> sortSegmentResult(Map<String,Integer> wordsFrenMaps,int topWordsCount){
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>();

        List<Map.Entry<String, Integer>> wordFrenList = new ArrayList<Map.Entry<String, Integer>>(wordsFrenMaps.entrySet());
        Collections.sort(wordFrenList, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) {
                return obj2.getValue() - obj1.getValue();
            }
        });
        for(int i=0;i<topWordsCount&&i<wordFrenList.size();i++){
            Map.Entry<String,Integer> wordFrenEntry=wordFrenList.get(i);
            if(wordFrenEntry.getValue()>1){
                list.add(wordFrenEntry);
                System.out.println(wordFrenEntry.getKey()+"-的次数为"+wordFrenEntry.getValue());
            }
        }

        return list;
    }

    //统计相关词频
    private static Map<String,Integer> getRelatedWordsDef(String conntent,Set<String> set){
        Map<String, Integer> wordsFren=new HashMap<String, Integer>();
        //需要优化
//        String[] strs = Client.indexFc(conntent).split(" ");
        String[] strs= FcTool.getFc().indexFc(conntent).split(" ");
        for (int i = 0;i < strs.length;i++){
            if (strs[i].length()<=1){
                continue;
            }
            if (!set.contains(strs[i])){
                continue;
            }
            if (i>0&&!set.contains(strs[i-1])){
               putWordsDef(wordsFren,strs[i-1],strs[i],LEFT_WORD);
            }
            if (i<strs.length-1&&!set.contains(strs[i+1])){
                putWordsDef(wordsFren,strs[i+1],strs[i],RIGHT_WORD);
            }
        }
        return wordsFren;
    }

    private static final int LEFT_WORD=03424234;
    private static final int RIGHT_WORD=03424235;

    /**
     * 
     * @param wordsFren
     * @param word
     * @param key
     * @param type
     * @return
     */
    private static Map<String,Integer> putWordsDef(Map<String, Integer> wordsFren,String word,String key,int type){
        if (type==LEFT_WORD){
          putWordsDef(wordsFren,word+key);
        }else{
          putWordsDef(wordsFren,key+word);
        }
        return wordsFren;
    }

    /**
     *
     * @param wordsFren
     * @param word
     * @return
     */
    private static Map<String,Integer> putWordsDef(Map<String, Integer> wordsFren,String word){
        if (wordsFren.containsKey(word)){
           wordsFren.put(word,wordsFren.get(word)+1);
        }else{
           wordsFren.put(word,1);
        }
        return wordsFren;
    }
}
